﻿// Page2.cpp : 实现文件
//

#include "stdafx.h"
#include "DmTool.h"
#include "Page2.h"
#include "afxdialogex.h"
#include "CppSQLite3.h"

// CPage2 对话框

IMPLEMENT_DYNAMIC(CPage2, CDialogEx)

CPage2::CPage2(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_PAGE2, pParent)
{

}

CPage2::~CPage2()
{
}

void CPage2::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CMB_PAGE2_ENDNAME, m_cmb_endTask);

	DDX_Control(pDX, IDC_LST_PAGE3_OCRLIST, m_lst_ocrList);
	DDX_Control(pDX, IDC_LST_PAGE2_OCR, m_lst_page2_ocrDetail);
}


BEGIN_MESSAGE_MAP(CPage2, CDialogEx)
	ON_LBN_SETFOCUS(IDC_LST_PAGE2_OCR, &CPage2::OnLbnSetfocusLstPage2Ocr)
	ON_LBN_SELCHANGE(IDC_LST_PAGE2_OCR, &CPage2::OnLbnSelchangeLstPage2Ocr)
	ON_BN_CLICKED(IDC_BTN_PAGE2_SAVE, &CPage2::OnBnClickedBtnPage2Save)
	ON_BN_CLICKED(IDC_BTN_PAGE2_TEST, &CPage2::OnBnClickedBtnPage2Test)
	ON_BN_CLICKED(IDC_BTN_PAGE2_DEL, &CPage2::OnBnClickedBtnPage2Del)
	ON_NOTIFY(NM_DBLCLK, IDC_LST_PAGE2_OCRLIST, &CPage2::OnNMDblclkLstPage2Ocrlist)
END_MESSAGE_MAP()


// CPage2 消息处理程序


BOOL CPage2::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	int nIndex = 0;

	m_lst_ocrList.InsertColumn(nIndex++, _T("编号"), LVCFMT_LEFT, 50);
	m_lst_ocrList.InsertColumn(nIndex++, _T("模块名称"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("识别数据"), LVCFMT_LEFT, 250);
	m_lst_ocrList.InsertColumn(nIndex++, _T("结束识别"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("识别次数"), LVCFMT_LEFT, 100);
	m_lst_ocrList.InsertColumn(nIndex++, _T("延时"), LVCFMT_LEFT, 100);

	//。//int nLen = sizeof(arrTask) / sizeof(arrTask[0]);

	//m_cmb_ocrTask.Clear();
	//for (int i = 0; i < nLen; i++)
	//{
	//	mapTask[arrTask[i]] = m_cmb_ocrTask.InsertString(i, arrTask[i]);

	//}

	m_lst_ocrList.SetBkColor(RGB(205, 226, 252));
	m_lst_ocrList.SetTextBkColor(RGB(205, 226, 252));
	m_lst_ocrList.SetTextColor(RGB(0, 0, 255));
	m_lst_ocrList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CPage2::ReLoad()
{
	//AfxMessageBox("sss");
	//page1->ReLoad();
	//((CPage2*)((CTabSheet*)GetDlgItem(IDC_TAB))->m_TabPages.at(1).m_pages)->ReLoad();
	CListBox* list = (CListBox*)GetDlgItem(IDC_LST_PAGE2_OCR);


	CppSQLite3DB db;
	CFileFind fFind;
	m_cmb_endTask.ResetContent();
	list->ResetContent();
	if (fFind.FindFile(_T("DMTool.db")))
	{
		db.open("DMTool.db", "ppo00123");
		CppSQLite3Query query = db.execQuery("SELECT * FROM ocrDetail order by id desc");
		//CMap<CString, CString, OcrInfo, OcrInfo> ocrCmp1;
		int i = 0;
		while (!query.eof())
		{
			mapocr[Str(query.fieldValue("name"))] = m_cmb_endTask.AddString(Str(query.fieldValue("name")));;
			list->AddString(Str(query.fieldValue("name")));
			query.nextRow();
		}
		m_lst_ocrList.DeleteAllItems();
		query = db.execQuery("SELECT * FROM ocr where status='0' order by id desc");
		while (!query.eof())
		{
			int nIndex = m_lst_ocrList.GetItemCount();
			m_lst_ocrList.InsertItem(nIndex, Str(query.fieldValue("id")));
			int nI = 1;
			m_lst_ocrList.SetItemText(nIndex, nI++, Str(query.fieldValue("startName")));
			m_lst_ocrList.SetItemText(nIndex, nI++, Str(query.fieldValue("ocrDetail")));
			m_lst_ocrList.SetItemText(nIndex, nI++, Str(query.fieldValue("endName")));
			m_lst_ocrList.SetItemText(nIndex, nI++, Str(query.fieldValue("count")));
			float delay = iVal(Str(query.fieldValue("delay")));
			CString str;
			str.Format(L"%.f", delay);
			m_lst_ocrList.SetItemText(nIndex, nI++, str);
			query.nextRow();

		}
		db.close();
	}
}



void CPage2::OnLbnSetfocusLstPage2Ocr()
{

}


void CPage2::OnLbnSelchangeLstPage2Ocr()
{
	// TODO: 在此添加控件通知处理程序代码
	// TODO: 在此添加控件通知处理程序代码
	CString str, str1;

	m_lst_page2_ocrDetail.GetCurSel();
	m_lst_page2_ocrDetail.GetText(m_lst_page2_ocrDetail.GetCurSel(), str1);

	if (!cList.Find(str1)) {
		cList.AddTail(str1);
	}
	else {
		return;
	}
	int i = 0;

	POSITION pos = cList.GetTailPosition();
	while (i<cList.GetCount())
	{
		CString m;
		if (i == 0) {
			str += cList.GetAt(pos);
		}
		else {
			//str += "," + cList.GetAt(pos);
			m = cList.GetAt(pos);
			str = m + "," + str;
		}
		i++;
		cList.GetPrev(pos);
		//cList.GetNext(pos);
	}
	SetDlgItemText(IDC_EDT_PAGE2_OCRLIST, str);
}


void CPage2::OnBnClickedBtnPage2Save()
{

	// TODO: 在此添加控件通知处理程序代码
	CString startName, endName, socrDetail, count;

	GetDlgItemText(IDC_EDT_PAGE2_NAME, startName);
	if (startName == L"" || startName.GetLength() == 0) {
		AfxMessageBox(L"请输入模块名称");
		return;
	}

	if (m_cmb_endTask.GetCurSel()>-1)
		m_cmb_endTask.GetLBText(m_cmb_endTask.GetCurSel(), endName);
	GetDlgItemText(IDC_EDT_PAGE2_COUNT, count);
	GetDlgItemText(IDC_EDT_PAGE2_OCRLIST, socrDetail);
	if (socrDetail == L"" || socrDetail.GetLength() == 0) {
		AfxMessageBox(L"请选择识别数据");
		return;
	}
	CppSQLite3DB db;
	CFileFind fFind;
	CString strInfo;
	try
	{
		db.open("DMTool.db", "ppo00123");
		CString strSql;
		strSql.Format(L"select startname from ocr where startname='%s'", startName);
		CString sResult = db.execQueryOne(StrP(strSql));

		CString sDelay;
		GetDlgItemText(IDC_EDT_PAGE2_DELAY, sDelay);

		CString taskName;
		if (m_ocrID == L"") {
			if (sResult != L"" && sResult.GetLength()>0) {
				AfxMessageBox(L"输入的名称重复!");
				return;
			}
			strSql.Format(L"insert into ocr(startName,ocrDetail,endName,count,delay,status) values('%s','%s','%s','%s','%s','0')",
				startName, socrDetail, endName, count, sDelay);
		}
		else {
			strSql.Format(L"update ocr set startName='%s',ocrDetail='%s',endName='%s',count='%s',delay='%s' where id=%s", startName, socrDetail, endName, count, sDelay, m_ocrID);
		}

		int nAffected = db.execDML(StrP(strSql));
		SetDlgItemText(IDC_EDT_PAGE2_NAME, L"");
		SetDlgItemText(IDC_EDT_PAGE2_DELAY, L"");
		SetDlgItemText(IDC_EDT_PAGE2_COUNT, L"");
		SetDlgItemText(IDC_EDT_PAGE2_OCRLIST, L"");
		m_cmb_endTask.SetCurSel(-1);
	}
	catch (const std::exception&e)
	{
		db.close();

	}
	db.close();
	ReLoad();
}


void CPage2::OnBnClickedBtnPage2Test()
{
	// TODO: 在此添加控件通知处理程序代码
	CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LST_PAGE2_OCRLIST);

	int nIndex = pList->GetSelectionMark();
	CString str = pList->GetItemText(nIndex, 2);
	CStringArray arr;
	//识别次数
	int nCount = iVal(pList->GetItemText(nIndex, 4));
	nCount = nCount == 0 ? 1 : nCount;
	//
	int delay = iVal(pList->GetItemText(nIndex, 5));
	CString sEndTask = pList->GetItemText(nIndex, 3);

	//OcrInfo ocrinfo = ocrCmp[];

	SplitStr(str, L",", arr);
	bool(*pn[])(OcrDetail) = { CmpCol,FindCol,FindMulCol,FindPic,FindStr };
	//BY_OCRACTION ocr[5] = { CmpCol,FindCol,FindMulCol,FindPic,FindStr };
	for (int j = 0; j < nCount; j++)
	{
		for (int i = 0; i < arr.GetCount(); i++)
		{
			OcrDetail oceDetail = ocrMap[arr[i]];
			CString str;
			str.Format(L"%s", oceDetail.sAction);
			int nIndex = iVal(str);
			pn[nIndex](oceDetail);
			if (sEndTask != "") {
				OcrDetail endOcrDetail = ocrMap[sEndTask];
				str.Format(L"%s", endOcrDetail.sAction);
				nIndex = iVal(str);
				if (pn[nIndex](endOcrDetail)) {
					AfxMessageBox(L"测试已完成");
					return;
				}
			}
		}
		g_pDmLD->delay(delay);
	}
}


void CPage2::OnBnClickedBtnPage2Del()
{
	// TODO: 在此添加控件通知处理程序代码
	CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LST_PAGE2_OCRLIST);
	int nSel = pList->GetSelectionMark();
	if (nSel < 0) {
		AfxMessageBox(_T("请选中一行在删除！"));
		return;
	}
	CppSQLite3DB db;
	CFileFind fFind;
	CString strID = pList->GetItemText(nSel, 0);
	if (fFind.FindFile(_T("DMTool.db")))
	{
		if (AfxMessageBox(L"你确定删除吗？", MB_YESNO) == IDYES)
		{
			db.open("DMTool.db", "ppo00123");
			CString strSql;
			strSql = L"update ocr set status='1' where id=" + strID;
			int nAffected = db.execDML(StrP(strSql));
			db.close();
			ReLoad();
		}
	}
}


void CPage2::OnNMDblclkLstPage2Ocrlist(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LST_PAGE2_OCRLIST);

	int nIndex = pList->GetSelectionMark();
	if (nIndex < 0) {
		return;
	}
	m_ocrID = pList->GetItemText(nIndex, 0);
	SetDlgItemText(IDC_EDT_PAGE2_NAME, pList->GetItemText(nIndex, 1));
	SetDlgItemText(IDC_EDT_PAGE2_COUNT, pList->GetItemText(nIndex, 4));
	SetDlgItemText(IDC_EDT_PAGE2_DELAY, pList->GetItemText(nIndex, 5));
	int nI = mapocr[pList->GetItemText(nIndex, 3)];
	m_cmb_endTask.SetCurSel(pList->GetItemText(nIndex, 3) == "" ? -1 : nI);
	nI = mapTask[pList->GetItemText(nIndex, 1)];

	CString str1 = pList->GetItemText(nIndex, 2);
	CString str;
	cList.RemoveAll();
	int istart = 0;
	while (istart != -1) {
		if (str1 == L"" && str1.GetLength() == 0)
			break;
		CString m;
		m = str1.Tokenize(L",", istart);
		if (m == L"" || m.GetLength() == 0)
			return;
		cList.AddTail(m);
	}
	//if (!cList.Find(str1)) {
	//	cList.AddTail(str1);
	//}
	//int i = 0;
	//
	//POSITION pos = cList.GetTailPosition();
	//while (i<cList.GetCount())
	//{
	//	if (i == 0) {
	//		str += cList.GetAt(pos);
	//	}
	//	else {
	//		str = cList.GetAt(pos) + "," + str;
	//	}

	//	i++;
	//	cList.GetPrev(pos);
	//}
	SetDlgItemText(IDC_EDT_PAGE2_OCRLIST, str1);
	*pResult = 0;
}
